<?php



class keke_user_class {
	
	
	static function get_user_info($username, $isuid = 1) {
		global $_UC, $basic_config;
		if ($basic_config ['user_intergration'] == 1) {
			return kekezu::get_user_info ( $username, ! $isuid );
			;
		} elseif ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			$uc_info = uc_get_user ( $username, $isuid );
			return $uc_info;
		} elseif ($basic_config ['user_intergration'] == 3) {
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			return uc_user_get ( $username, $isuid );
		}
	
	}
	
	static function user_register($username, $password, $email) {
		global $_UC, $basic_config;
		if ($basic_config ['user_intergration'] == 1) {
			$member_obj = new Keke_witkey_member_class ();
			$member_obj->setEmail ( $email );
			$member_obj->setUsername ( $username );
			$member_obj->setPassword ( md5 ( $password ) );
			$res_uid = $member_obj->create_keke_witkey_member ();
			$space_obj = new Keke_witkey_space_class ();
			$space_obj->setUid ( $res_uid );
			$space_obj->setUsername ( $username );
			$space_obj->setPassword ( md5 ( $password ) );
			$space_obj->setEmail ( $email );
			$space_obj->setReg_time ( time () );
			$space_obj->setReg_ip ( kekezu::get_ip () );
			$res_space_id = $space_obj->create_keke_witkey_space ();
			if ($res_uid && $res_space_id) {
				return array ('uid' => $res_uid, 'username' => $username, 'email' => $email );
			} else {
				return false;
			}
		
		} elseif ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			$reg_uid = uc_user_register ( $username, $password, $email );
			if ($reg_uid > 0) {
				$member_obj = new Keke_witkey_member_class ();
				$member_obj->setUid ( $reg_uid );
				$member_obj->setEmail ( $email );
				$member_obj->setUsername ( $username );
				$member_obj->setPassword ( md5 ( $password ) );
				$res_uid = $member_obj->create_keke_witkey_member ();
				
				$space_obj = new Keke_witkey_space_class ();
				$space_obj->setUid ( $res_uid );
				$space_obj->setUsername ( $username );
				$space_obj->setPassword ( md5 ( $password ) );
				$space_obj->setEmail ( $email );
				$space_obj->setReg_time ( time () );
				$space_obj->setReg_ip ( kekezu::get_ip () );
				$res_space_id = $space_obj->create_keke_witkey_space ();
				
				return array ('uid' => $res_uid, 'username' => $username, 'email' => $email );
			} else {
				return false;
			}
		} elseif ($basic_config ['user_intergration'] == 3) {
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			$reg_uid = uc_user_register ( $username, md5 ( $password ), $email );
			if ($reg_uid > 0) {
				$member_obj = new Keke_witkey_member_class ();
				$member_obj->setUid ( $reg_uid );
				$member_obj->setEmail ( $email );
				$member_obj->setUsername ( $username );
				$member_obj->setPassword ( md5 ( $password ) );
				$res_uid = $member_obj->create_keke_witkey_member ();
				
				$space_obj = new Keke_witkey_space_class ();
				$space_obj->setUid ( $res_uid );
				$space_obj->setUsername ( $username );
				$space_obj->setPassword ( md5 ( $password ) );
				$space_obj->setEmail ( $email );
				$space_obj->setReg_time ( time () );
				$space_obj->setReg_ip ( kekezu::get_ip () );
				$res_space_id = $space_obj->create_keke_witkey_space ();
				
				return array ('uid' => $res_uid, 'username' => $username, 'email' => $email );
			} else {
				return false;
			}
		}
	}
	
	static function user_login($username, $password) {
		global $basic_config;
		$member_obj = new Keke_witkey_member_class ();
		$member_obj->setWhere ( "username='$username'" );
		$user_arr = $member_obj->query_keke_witkey_member ();
		$user_arr = $user_arr [0];
		
		if ($basic_config ['user_intergration'] == 1) { 
			$user_arr ['username'] = ($user_arr ['username'] == $username) ? $username : 0;
			$user_arr ['uid'] = ($user_arr ['password'] == md5 ( $password )) ? $user_arr ['uid'] : 0;
			return array ('0' => $user_arr ['uid'], '1' => $user_arr ['username'] );
		
	
		} elseif ($basic_config ['user_intergration'] == 2) { 
			require_once S_ROOT . './keke_client/ucenter/client.php';
			$uc_info = uc_user_login ( $username, md5 ( $password ) );
			list ( $uc_uid, $uc_username, $uc_password, $uc_email ) = $uc_info;
	
			if ($uc_uid > 0) {
	
				$userinfo = kekezu::get_user_info ( $uc_uid );
				$member_obj = new Keke_witkey_member_class ();
				$member_obj->setUid ( $uc_uid );
				$member_obj->setEmail ( $uc_email );
				$member_obj->setUsername ( $uc_username );
				$member_obj->setPassword ( $uc_password );
			
				if (! $userinfo) {
					
					$member_obj->create_keke_witkey_member ();
				} else {
					
					$member_obj->edit_keke_witkey_member ();
				}
				
				$space_obj = new Keke_witkey_space_class ();
				$space_obj->setUid ( $uc_uid );
				$space_obj->setUsername ( $uc_username );
				$space_obj->setPassword ( $uc_password );
				$space_obj->setEmail ( $uc_email );
				$space_obj->setReg_time ( time () );
				$space_obj->setReg_ip ( kekezu::get_ip () );
				
				if (! $userinfo) {
					$space_obj->create_keke_witkey_space ();
				} else {
					$space_obj->edit_keke_witkey_space ();
				}
			}
			return $uc_uid > 0 ? $uc_info : $uc_uid;
		} elseif ($basic_config ['user_intergration'] == 3) {
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			$uc_info = uc_user_login ( $username, md5 ( $password ), 0 );
			
			if ($uc_info ['status'] != 1) {
				return false;
			}
			
			$uc_uid = $uc_info ['uid'];
			$uc_username = $uc_info ['username'];
			$uc_email = $uc_info ['email'];
			if ($uc_uid > 0) {
		
				$userinfo = kekezu::get_user_info ( $uc_uid );
				$member_obj = new Keke_witkey_member_class ();
				$member_obj->setUid ( $uc_uid );
				$member_obj->setEmail ( $uc_email );
				$member_obj->setUsername ( $uc_username );
				$member_obj->setPassword ( md5 ( $password ) );
		
				if (! $userinfo) {
					
					$member_obj->create_keke_witkey_member ();
				} else {
					
					$member_obj->edit_keke_witkey_member ();
				}
				
				$space_obj = new Keke_witkey_space_class ();
				$space_obj->setUid ( $uc_uid );
				$space_obj->setUsername ( $uc_username );
				$space_obj->setPassword ( md5 ( $password ) );
				$space_obj->setEmail ( $uc_email );
				$space_obj->setReg_time ( time () );
				$space_obj->setReg_ip ( kekezu::get_ip () );
				
				if (! $userinfo) {
					$space_obj->create_keke_witkey_space ();
				} else {
					$space_obj->edit_keke_witkey_space ();
				}
			}
			$uc_info = array ($uc_uid, $uc_username );
			return $uc_uid > 0 ? $uc_info : $uc_uid;
		}
	
	}
	
	static function user_edit($username, $oldpwd, $newpwd, $email, $nocheckold = 1, $uid = '') {
		global $_UC, $basic_config;
		if ($basic_config ['user_intergration'] == 1) {
			return 1;
		} elseif ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			return uc_user_edit ( $username, $oldpwd, $newpwd, $email, $nocheckold );
		} elseif ($basic_config ['user_intergration'] == 3) {
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			return uc_user_edit ( $uid, $username, $newpw, $email );
		}
	
	}
	
	static function user_delete($uid) {
		global $_UC, $basic_config;
		if ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			uc_user_delete ( $uid );
		} elseif ($basic_config ['user_intergration'] == 3) { 
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			return uc_user_delete ( $uid );
		}
	}
	
	static function user_synlogin($uid,$pwd=null) {
		global $_UC, $basic_config;
		if ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			return uc_user_synlogin ( $uid );
		} elseif ($basic_config ['user_intergration'] == 3) { 
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			 
			$syn_arr = uc_user_login ( $uid, $pwd, 1 );
			return $syn_arr ['synlogin'];
		}
	}
	
	static function user_synlogout() {
		global $_UC, $basic_config;
		
		if ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			return uc_user_synlogout ();
		} elseif ($basic_config ['user_intergration'] == 3) { 
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			return uc_user_synlogout ();
		}
	}
	
	static function user_checkemail($email) {
		global $_UC, $basic_config;
		if ($basic_config ['user_intergration'] == 1) {
			$member_obj = new Keke_witkey_member_class ();
			$member_obj->setWhere ( 'email="' . $email . '"' );
			$res = $member_obj->count_keke_witkey_member ();
			$res = $res == 0 ? 1 : - 6;
		
		} elseif ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			$res = uc_user_checkemail ( $email );
		
		} elseif ($basic_config ['user_intergration'] == 3) { 
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			$res = uc_check_email ( $email );
		}
		return $res;
	}
	
	static function user_checkname($username) {
		global $_UC, $basic_config;
		if ($basic_config ['user_intergration'] == 1) {
			$member_obj = new Keke_witkey_member_class ();
			$member_obj->setWhere ( 'username="' . $username . '"' );
			$res = $member_obj->count_keke_witkey_member ();
			$res = $res ? 0 : 1;
		} elseif ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			$res = uc_user_checkname ( $username );
		} elseif ($basic_config ['user_intergration'] == 3) { 
			require_once (S_ROOT . './keke_client/pw_client/uc_client.php');
			$res = uc_check_username ( $username );
			 
		}
		
		return $res;
	
	}
	
	static function user_getprotected() {
		global $_UC, $basic_config;
		if ($basic_config_info ['ban_users']) {
			$limit_username = explode ( ',', $basic_config_info ['ban_users'] );
		}
		
		if ($basic_config ['user_intergration'] == 1) {
			return $limit_username;
		} elseif ($basic_config ['user_intergration'] == 2) {
			require_once S_ROOT . './keke_client/ucenter/client.php';
			$limit_username = $limit_username ? $limit_username : array ();
			uc_user_getprotected ();
			
			return array_merge ( uc_user_getprotected (), $limit_username );
		}
	
	}
	
	static function get_user_pic($uid, $size = 'small') {
		global $basic_config;
		if (! in_array ( $size, array ('big', 'middle', 'small' ) )) {
			$size = 'small';
		}
		if ($basic_config ['user_intergration'] == 1) {
			return '<img src="data/uploads/member/' . $size . '_' . $uid . '.jpg" class="pic_' . $size . '" onerror="this.src=\'resource/img/avgdefault' . $size . '.jpg\'" />';
		} elseif ($basic_config ['user_intergration'] == 2) {
			require S_ROOT . './keke_client/ucenter/client.php';
			return "<img src=" . UC_API . "/avatar.php?uid=$uid&size=$size\">";
		}
	
	}
	
	static function get_user_auth($uid,$auth_code=null){
		
		$sql="select a.* from ".TABLEPRE."witkey_auth_item as a left join ".TABLEPRE."witkey_auth_record as b on a.auth_id=b.auth_id where b.auth_uid='$uid' ";
		if($auth_code){
			$sql.=" and a.auth_dir= '$auth_code' and b.auth_status=1 ";
		}
		$res=db_factory::query($sql);
		if($res){
			if($auth_code){
				return true;
			}else{
				return $res;
			}
		}
		else{
				return false;
		}
	}

}